package com.huilan.hgt.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 过滤器
 *
 * @author ghy
 * @date 2018-11-03 9:52 PM
 **/
@WebFilter(filterName = "pathFilter",urlPatterns = "/*")
public class PathFilter implements Filter{

    @Override
    public void init(FilterConfig config) throws ServletException {
        // TODO Auto-generated method stub
    }
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse resp = (HttpServletResponse) response;
        String path = req.getContextPath();
        req.setAttribute("ctx", path);
        String pathInfo = req.getRequestURI();
        if (pathInfo == null || pathInfo.equals(path) || pathInfo.equals(path + "/") || "".equals(pathInfo)) {
            resp.sendRedirect(path+"/index.do");
            return;
        }
        chain.doFilter(request, response);

    }


    @Override
    public void destroy() {
        // TODO Auto-generated method stub

    }
}
